var f = function (a) { return document.getElementById(a); }

function getClassNames(classStr, tagName) {
    if (document.getElementsByClassName) {
        return document.getElementsByClassName(classStr)
    } else {
        var nodes = document.getElementsByTagName(tagName), ret = [];
        for (i = 0; i < nodes.length; i++) {
            if (hasClass(nodes[i], classStr)) {
                ret.push(nodes[i])
            }
        }
        return ret;
    }
}
function hasClass(tagStr, classStr) {
    var arr = tagStr.className.split(/\s+/);  //这个正则表达式是因为class可以有多个,判断是否包含  
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == classStr) {
            return true;
        }
    }
    return false;
}
var f1 = function (tagclass, tagname) { return getClassNames(tagclass, tagname); }

function pageNow(tag) {//取到当前页
    var ctpage;
    var temp = f(tag).innerHTML;
    temp = temp.replace("第", "").replace("页", "");
    ctpage = parseInt(temp);
    return ctpage;
}
function calShowCount(p, siz, tot) {//计算起始和结束显示第几条
    var stp;
    var enp;
    var sct = (p - 1) * siz + 1;
    var ect = p * siz;
    ect = (ect > tot) ? tot : ect;
    return { stp: sct, enp: ect };
}
function isShowDiv(tot, start, end, divs) {//是否显示
    for (var i = 1; i < (tot + 1); i++) {
        if (i >= start && i <= end) {
            divs[i - 1].style.display = "block";
        } else {
            divs[i - 1].style.display = "none";
        }
    }

}
function getQueryString(name) {  //地址连接参数，返回 
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  
    var r = window.location.search.substr(1).match(reg);  
    if (r != null) return unescape(r[2]);  
    return null;  
}

function showBlog() {
    var test;
    var totals = 0;//总记录
    var totalPage = 0;//总页数
    var pagesize = 5;//每页显示几个
    var currentpage = 1;
    f('jiazai').innerHTML="加载中...";
    var cur=getQueryString("page");
    if(cur){
        currentpage=cur;
    }

    if (window.XMLHttpRequest) {
        test = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        test = new window.ActiveXObject();
    } else {
        alert("请升级至最新版本的浏览器");
    }
    if (test != null) {
        test.open("GET", "./searchdata/bloglists.json", true);
        test.send(null);
        test.onreadystatechange = function () {
            if (test.readyState == 4 && test.status == 200) {
                var obj = JSON.parse(test.responseText);
                 totals = obj.bloglinks.length;
                 let newbloglinks=obj.bloglinks.reverse()
                f('jiazai').innerHTML="";
                for (var i in obj.bloglinks) {
                    f('conte').innerHTML += "<div class='frame'>" +
                        "<div class='title'><h3><a href=./blogs/" + newbloglinks[i].link + ">" + newbloglinks[i].title + "</a></h3></div>" +
                        "<div class='overview'>" + newbloglinks[i].oview + "</div>" +
                        "<div class='dtime'>时间：" + newbloglinks[i].date + "</div></div>"
                }

                if (!totals) {
                    f('conte').innerHTML = "没有博客。";
                    return;
                }

                //总共分几页 
                if (totals / pagesize > parseInt(totals / pagesize)) {
                    totalPage = parseInt(totals / pagesize) + 1;
                } else {
                    totalPage = parseInt(totals / pagesize);
                }
                f('ye').innerHTML = "总页：" + totalPage;

                var isshow = f1("frame", "div");

                var tcount = calShowCount(currentpage, pagesize, totals);
                var startcount = tcount.stp;
                var endcount = tcount.enp;
                isShowDiv(totals, startcount, endcount, isshow);
                f('dye').innerHTML = "第" + currentpage + "页";

                f('pre').onclick = function () {//上一页
                    currentpage = pageNow('dye');
                    if (currentpage > 1) {
                        currentpage = currentpage - 1;
                        var tcount = calShowCount(currentpage, pagesize, totals);
                        var startcount = tcount.stp;
                        var endcount = tcount.enp;
                        isShowDiv(totals, startcount, endcount, isshow);
                        f('dye').innerHTML = "第" + currentpage + "页";
                        location.href="./bloglist.html?page="+currentpage;
                    } else {
                        alert("已经是首页!");
                    }

                }

                f('next').onclick = function () {//下一页
                    currentpage = pageNow('dye');
                    if (currentpage < totalPage) {
                        currentpage = currentpage + 1;
                        var tcount = calShowCount(currentpage, pagesize, totals);
                        var startcount = tcount.stp;
                        var endcount = tcount.enp;
                        isShowDiv(totals, startcount, endcount, isshow);
                        f('dye').innerHTML = "第" + currentpage + "页";
                        location.href="./bloglist.html?page="+currentpage;
                    } else {
                        alert("已经是尾页!");
                    }

                }

                /*触屏翻页*/
                var startX;
                try{
                document.addEventListener("touchstart", function (e) {
                    startX = e.touches[0].pageX;
                }, false);
                }catch(e){
                    //alert("浏览器版本过低！");
                }
                try{
                document.addEventListener("touchend", function (e) {
                    var endX;
                    endX = e.changedTouches[0].pageX;
                    currentpage = pageNow('dye');
                    if (endX - startX > 200) {
                        //alert("向右滑动");
                        if (currentpage > 1) {
                            currentpage = currentpage - 1;
                            var tcount = calShowCount(currentpage, pagesize, totals);
                            var startcount = tcount.stp;
                            var endcount = tcount.enp;
                            isShowDiv(totals, startcount, endcount, isshow);
                            f('dye').innerHTML = "第" + currentpage + "页";
                            location.href="./bloglist.html?page="+currentpage;
                        } else {
                            alert("已经是首页!");
                        }

                    }
                    if (endX - startX < -200) {
                        //alert("向左滑动");
                        if (currentpage < totalPage) {
                            currentpage = currentpage + 1;
                            var tcount = calShowCount(currentpage, pagesize, totals);
                            var startcount = tcount.stp;
                            var endcount = tcount.enp;
                            isShowDiv(totals, startcount, endcount, isshow);
                            f('dye').innerHTML = "第" + currentpage + "页";
                            location.href="./bloglist.html?page="+currentpage;
                        } else {
                            alert("已经是尾页!");
                        }
                    }
                }, false)
                }catch(e){
                    //alert("浏览器版本过低！");
                }
                /**/

                f('jumpbtn').onclick = function () {//跳转页
                    currentpage = f('jumppage').value;
                    var tcurrentpage = pageNow('dye');
                    if (currentpage.replace(/\s+/g, "") == 0) {
                        alert("请输入跳转页!");
                        return;
                    }
                    var re = /^[0-9]+$/;
                    if (!re.test(currentpage)) {//正整数
                        alert("输入数字!");
                        return;
                    }
                    if (currentpage > totalPage) {
                        alert("超过最大页数!");
                        return;
                    }
                    if (currentpage == tcurrentpage) {
                        alert("正在当前页!");
                        return;
                    }
                    var tcount = calShowCount(currentpage, pagesize, totals);
                    var startcount = tcount.stp;
                    var endcount = tcount.enp;
                    isShowDiv(totals, startcount, endcount, isshow);
                    f('dye').innerHTML = "第" + currentpage + "页";
                    location.href="./bloglist.html?page="+currentpage;

                }
                //
                document.onkeydown = function (event) {//回车跳页
                    var e = event || window.event;
                    if (e && e.keyCode == 13) { 
                         currentpage = f('jumppage').value;
                    var tcurrentpage = pageNow('dye');
                    if (currentpage.replace(/\s+/g, "") == 0) {
                        alert("请输入跳转页!");
                        return;
                    }
                    var re = /^[0-9]+$/;
                    if (!re.test(currentpage)) {//正整数
                        alert("输入数字!");
                        return;
                    }
                    if (currentpage > totalPage) {
                        alert("超过最大页数!");
                        return;
                    }
                    if (currentpage == tcurrentpage) {
                        alert("正在当前页!");
                        return;
                    }
                    var tcount = calShowCount(currentpage, pagesize, totals);
                    var startcount = tcount.stp;
                    var endcount = tcount.enp;
                    isShowDiv(totals, startcount, endcount, isshow);
                    f('dye').innerHTML = "第" + currentpage + "页";
                    location.href="./bloglist.html?page="+currentpage; 
                        }
                        };//


            }
        }

    }


}
window.onload = showBlog;